<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

/**
 *  17koko游戏公众号后台管理
 *  index.php
 *  lichaoliang  2015-01-29
 */

class Index extends Admin_Controller
{

    public function __construct()
    {
        $arr_not_check = array('login', 'but_login', 'sign_out');//不判断登录的方法
        parent::__construct($arr_not_check);
    }

    /**
     *  后台首页
     */
    public function index()
    {
        $this->load->view('admin/index.php');
    }

    /**
     *  登陆页面
     */
    public function login()
    {
        $this->load->view('admin/login.php');
    }

    /**
     *  后台登录提交页面
     */
    public function but_login()
    {
        $username = !empty($_POST['username']) ? addslashes(trim($_POST['username'])) : '';
        $pwd = !empty($_POST['pwd']) ? addslashes(trim($_POST['pwd'])) : '';
        $result = $this->admin_model->login($username, $pwd);

        if($result['errorCode']===0){
            $json['num'] = 1;
            $json['error'] = '登录成功！';

        }else{
            $json['num'] = 0;
            $json['error'] = '用户名或者密码错误！';
        }

        echo json_encode($json);
    }

    /**
     *  修改密码界面
     */
    public function update_passwd_tep()
    {
        $this->load->view('admin/update_passwd_tep.php');
    }

    /**
     *  修改密码 逻辑
     */
    public function update_passwd()
    {
        $admin_name = $this->session->userdata('admin_username');
        $pwd = !empty($_POST['pwd']) ? addslashes($_POST['pwd']) : '';
        $new_pwd = !empty($_POST['new_pwd']) ? addslashes($_POST['new_pwd']) : '';
        $password = !empty($_POST['password']) ? addslashes($_POST['password']) : '';

        if (strlen($new_pwd) < 7) {
            $json['num'] = 0;
            $json['error'] = '你输入的新密码少于7个字符！';
            echo json_encode($json);
            exit;
        }

        $row = $this->admin_model->get_name($admin_name);

        if ($row['passwd'] == md5($pwd)) {
            if ($new_pwd == $password) {
                $rs = $this->admin_model->change_passwd($admin_name, md5($password));

                if (!empty($rs)) {
                    $json['num'] = 1;
                    $json['error'] = '修改密码成功';
                    echo json_encode($json);
                    exit;
                } else {
                    $json['num'] = 0;
                    $json['error'] = '修改密码失败';
                    echo json_encode($json);
                    exit;
                }
            } else {
                $json['num'] = 0;
                $json['error'] = '两次密码不一致!';
                echo json_encode($json);
                exit;
            }
        } else {
            $json['num'] = 0;
            $json['error'] = '原始密码错误!';
            echo json_encode($json);
            exit;
        }

    }

    /**
     * 退出登录
     */
    public function sign_out()
    {
        $this->admin_model->logout();
        //echo "<script type='text/javascript'>window.location.href='" . site_url('admin/index/login') . "';</script>";
        alert_and_redirect('', 'admin/index/login');
    }


}